home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!xmission!news
- From: tknarr@xmission.com ( Todd Knarr )
- Newsgroups: comp.lang.c++
- Subject: Re: Date class
- Date: 4 Feb 1996 18:05:00 GMT
- Organization: Chaos Central
- Message-ID: <4f2sgc$bct@news.xmission.com>
- References: <4f09k2$nnu@george.rutgers.edu>
- Reply-To: tknarr@xmission.com ( Todd Knarr )
- NNTP-Posting-Host: slc68.xmission.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- Here's the .C file that completes the Date class.
-
- //
- // Classname : Date
- //
- // Author : Todd Knarr
- //
- // Description :
- // Provides a Date class which represents dates as Julian day numbers
- // ( days since 1 Jan 4713 BC ).
- // This class can handle all dates from 1 Jan 4713BC to 31 Dec 9999AD.
- //
- // Note: Years AD are positive, years BC are negative. There is
- // no year 0AD, it goes from 1BC to 1AD. A year of 0 will be treated
- // as 1BC. No attempt is made to validate ranges. Physical errors
- // will not result from insane day-month combinations like the 87th
- // day of the 45th month, but the results will obviously not make
- // much sense.
- //
- // Date conversion routines by Eric Bergman-Terrell,
- // Computer Language Dec 1990.
- //
-
- #include "date.h"
-
- #include <time.h>
-
- //
- // Function : YmdToJd
- //
- // Parameters : int year, month, day
- //
- // Return values : long julian day
- //
- // Description :
- //
- long Date::YmdToJd( const int iYear, const int iMonth, const int iDay )
- {
- long jul_day;
-
- int a,b;
- int year = iYear, month = iMonth, day = iDay;
- float year_corr;
-
- if ( year < 0 )
- year++;
- year_corr = ( year > 0 ? 0.0 : 0.75 );
- if ( month <= 2 )
- {
- year--;
- month += 12;
- }
- b = 0;
- if ( year * 10000.0 + month * 100.0 + day >= 15821015.0 )
- {
- a = year / 100;
- b = 2 - a + a / 4;
- }
- jul_day = (long) ( 365.25 * year - year_corr ) +
- (long) ( 30.6001 * ( month + 1 ) ) + day + 1720995L + b;
-
- return jul_day;
- }
-
- //
- // Function : JdToYmd
- //
- // Parameters : long julian day, pointers to int year, month, day
- //
- // Return values : none
- //
- // Description :
- //
- void Date::JdToYmd( const long lJD, int *piYear, int *piMonth, int *piDay )
- {
- long a, b, c, d, e, z, alpha;
-
- z = lJD;
- if ( z < 2299161L )
- a = z;
- else
- {
- alpha = (long) ( ( z - 1867216.25 ) / 36524.25 );
- a = z + 1 + alpha - alpha / 4;
- }
- b = a + 1524;
- c = (long) ( ( b - 122.1 ) / 365.25 );
- d = (long) ( 365.25 * c );
- e = (long) ( ( b - d ) / 30.6001 );
- *piDay = (int) b - d - (long) ( 30.6001 * e );
- *piMonth = (int) ( e < 13.5 ) ? e - 1 : e - 13;
- *piYear = (int) ( *piMonth > 2.5 ) ? ( c - 4716 ) : c - 4715;
- if ( *piYear <= 0 )
- *piYear -= 1;
-
- return;
- }
-
- //
- // Function : ToString
- //
- // Parameters : pointer to string buffer to fill in
- //
- // Return values : none
- //
- // Description : Formats the Date into an ASCII representation.
- // This is the ASCII form of the long Julian day number.
- // The string is a fixed-length 11-character string, including
- // the NUL terminator;
- //
- void Date::ToString( char *szBuffer ) const
- {
- int i;
- long Temp;
-
- Temp = lJulianDay;
- if ( Temp < 0L )
- szBuffer[0] = '-';
- else
- szBuffer[0] = '+';
- szBuffer[10] = '\0';
- for ( i = 9; i > 0; i-- )
- {
- szBuffer[i] = ( Temp % 10L ) + '0';
- Temp /= 10;
- }
-
- return;
- }
-
- //
- // Function : DayOfYear
- //
- // Parameters : none
- //
- // Return values : day within year
- //
- // Description :
- //
- int Date::DayOfYear( void ) const
- {
- int y, m, d;
- long soy;
-
- JdToYmd( lJulianDay, &y, &m, &d );
- soy = YmdToJd( y, 1, 1 );
- return (int) ( lJulianDay - soy + 1 );
- }
-
- //
- // Function : ToSysTime
- //
- // Parameters : none
- //
- // Return values : converted result
- //
- // Description : Converts the date to a time_t value
- // representing midnight of that date.
- //
- time_t Date::ToSysTime( void ) const
- {
- struct tm tmRep;
- int y, m, d;
- time_t t;
-
- JdToYmd( lJulianDay, &y, &m, &d );
- if ( y < 1970 )
- {
- y = 1970;
- m = 1;
- d = 1;
- }
- tmRep.tm_year = y - 1900 ;
- tmRep.tm_mon = m;
- tmRep.tm_mday = d;
- tmRep.tm_hour = 0;
- tmRep.tm_min = 0;
- tmRep.tm_sec = 0;
- tmRep.tm_isdst = 0;
-
- t = mktime( &tmRep );
- return t;
- }
-
- --
- Todd Knarr : tknarr@xmission.com | finger for PGP public key
- | Member, USENET Cabal
-
- Seriously, I don't want to die just yet. I don't care how
- good-looking they are, I! don't! want! to! die!"
- -- Megazone ( UF1 )
-
-